<?php


//MD, VA, DC, DE, FL Insurance Cost
 class TitleInsuranceCosts  {
    private $rateInfoProperties;
    private $titleInsuranceCalculatorHelper;
    private $state;

    public function TitleInsuranceCosts($state, $rateInfoProperties) 
    {
       $this->state = $state;
       $this->rateInfoProperties = $rateInfoProperties;
       
       $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
    }

    public function getTitleServicesCost() {
        return $this->rateInfoProperties->getTitleServicesCost($this->state);
    }

     public function  getSettlementFeeCost() {
        return $this->rateInfoProperties->getSettlementFeeCost($this->state);
    }

    public function getLocationSurveyCost() {
        return $this->rateInfoProperties->getLocationSurveyCost($this->state);
    }

    public function getLendersReissueCost($thousands) {
        $tierCosts = array();
//        tierCosts.add(new TierCost(250, 1.67));
//        tierCosts.add(new TierCost(500, 1.33));
//        tierCosts.add(new TierCost(1000, 1.16));
//        tierCosts.add(new TierCost(5000, 1.01));
//        tierCosts.add(new TierCost(15000, 0.79));
//        tierCosts.add(new TierCost(Integer.MAX_VALUE, 0.63));
        
        $max = 6;
        if ($this->state == EState::FL) $max = 4;
          
        for ($t = 1; $t <= $max; $t++) {
            $tierCosts[$t-1] = $this->rateInfoProperties->getTitleInsuranceLenderReissueTier($this->state, $t);
        }

        return $this->titleInsuranceCalculatorHelper->calculateTieredCost($thousands, $tierCosts, $this->rateInfoProperties->getTitleInsuranceLenderReissueMin($this->state));
    }
    
    public function getLendersCost($thousands) {
          $tierCosts = array();
//        tierCosts.add(new TierCost(250, 2.78));
//        tierCosts.add(new TierCost(500, 2.21));
//        tierCosts.add(new TierCost(1000, 1.94));
//        tierCosts.add(new TierCost(5000, 1.68));
//        tierCosts.add(new TierCost(15000, 1.31));
//        tierCosts.add(new TierCost(Integer.MAX_VALUE, 1.05));

          $max = 6;
          if ($this->state == EState::FL) $max = 5;
        for ($t = 1; $t <= $max; $t++) {
            $tierCosts[$t-1] =  $this->rateInfoProperties->getTitleInsuranceLenderTier($this->state, $t);
        }

        return $this->titleInsuranceCalculatorHelper->calculateTieredCost($thousands, $tierCosts, $this->rateInfoProperties->getTitleInsuranceLenderMin($this->state));
    }

    public function  getStandardOwnersCost($thousands) {
        $tierCosts =array();
//        tierCosts.add(new TierCost(250, 3.89));
//        tierCosts.add(new TierCost(500, 3.31));
//        tierCosts.add(new TierCost(1000, 2.78));
//        tierCosts.add(new TierCost(5000, 2.21));
//        tierCosts.add(new TierCost(15000, 1.84));
//        tierCosts.add(new TierCost(Integer.MAX_VALUE, 1.58));

        $max = 6;
        if ($this->state == EState::FL) $max = 5;
        
        for ($t = 1; $t <= $max; $t++) {
            $tierCosts[$t-1]= $this->rateInfoProperties->getTitleInsuranceStandardOwnerTier($this->state, $t);
        }
        return $this->titleInsuranceCalculatorHelper->calculateTieredCost($thousands, $tierCosts, $this->rateInfoProperties->getTitleInsuranceStandardOwnerMin($this->state));
    }

    public function getEnhancedOwnersCost($thousands) {
        //echo $this->getStandardOwnersCost($thousands); exit();
        //echo $this->rateInfoProperties->getTitleInsuranceEnhancedOwnerRatio(EState::MD); exit();
        return $this->getStandardOwnersCost($thousands) * $this->rateInfoProperties->getTitleInsuranceEnhancedOwnerRatio($this->state);
    }

    public function getSimultaneousIssueCost() {
        return $this->rateInfoProperties->getTitleInsuranceSimultaneousIssue($this->state);
    }
    public function getLendersReissueDiscountRate($thousands) {
        $reissueCost = $this->getLendersReissueCost($thousands);
        $lendersCost = $this->getLendersCost($thousands);
        //        System.out.println("LendersReissueCost discount % =" + discountPercentage);
        return 1.0 - ($reissueCost / $lendersCost);
    }

    public function getLendersReissueDiscount($thousands) {
        $lendersCost = $this->getLendersCost($thousands);
        $reissueCost = $this->getLendersReissueCost($thousands);
        return $lendersCost - $reissueCost;
    }
}
?>
